package com.lei.yohojava_002.presenter;

import android.util.Log;

import com.lei.library_mvp_retrofit.http.bean.LoginBean;
import com.lei.library_mvp_retrofit.http.bean.RegisterBean;
import com.lei.library_mvp_retrofit.http.ob.BaseObservable;
import com.lei.library_mvp_retrofit.http.ob.BaseObserver;
import com.lei.library_mvp_retrofit.mvp.presenter.BasePresenter;
import com.lei.library_mvp_retrofit.mvp.view.IView;
import com.lei.yohojava_002.LoginActivity;
import com.lei.yohojava_002.RegisterActivity;
import com.lei.yohojava_002.contract.YoHoContract;

import org.json.JSONObject;

import okhttp3.MediaType;
import okhttp3.RequestBody;

/**
 * @ProjectName : YoHoJava_002
 * @Author : 雷金泷
 * @Time : 2021/1/8 18:37
 * @Description : 文件描述
 */
public class YoHoPresenter extends BasePresenter<YoHoContract.Model, IView> implements YoHoContract.Presenter {
    private LoginActivity loginView;
    private RegisterActivity registerActivity;
    public YoHoPresenter(YoHoContract.Model mModel, YoHoContract.LoginView mView) {
        super(mModel, mView);
        loginView = (LoginActivity) mView;
    }
    public YoHoPresenter(YoHoContract.Model mModel, YoHoContract.RegisterView mView) {
        super(mModel, mView);
        registerActivity = (RegisterActivity) mView;
    }

    @Override
    public void login(String name) {
        //{"+"\"username\":\""+name+"\","+"\"password\":\""+pwd+"\","+"}
        //{"username":"我是雷金泷","password":"e6cEHh0CnxwqSAdw5thj71xadqerq0H8XhmyRXasvPGiZsuPFYFUzGdfnL1ySy92PqpL1jmRM6Vw\naIe6rTwUNX4J9oaGkoBWxWBv0SyIbiqEVj2rT92A0GcYtzDB0So7wbUkGOh+OeRHZdXdxJgFelyU\nZWwE57qy45ARamgfe1Y=\n"}
//        String request_login = "{\"username\":\""+name+"\","+"\"password\":"+pwd+"\"}";

        new BaseObservable().doIt(mModel.login(name), new BaseObserver<LoginBean>() {
            @Override
            public void onNext(LoginBean loginBean) {
                if (loginBean!=null){
                    loginView.getLogin(loginBean);
                }
            }

            @Override
            public void onError(Throwable e) {
                Log.e("ljl", "onError: "+e.getMessage() );
            }
        });
    }

    @Override
    public void register(String name) {
        new BaseObservable().doIt(mModel.register(name), new BaseObserver<RegisterBean>() {
            @Override
            public void onNext(RegisterBean registerBean) {
                if (registerBean!=null){
                    registerActivity.getReg(registerBean);
                }
            }

            @Override
            public void onError(Throwable e) {

            }
        });
    }

    @Override
    public void destroy() {

    }
}
